package com.whq.mvc.base.redis;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.Properties;

public class PropertiesUtils {
    final static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

    private static Properties properties = new Properties();

    static {
        logger.info("开始加载redis配置文件");
        loadProp();
    }

    private static void loadProp() {
        InputStream inputStream = null;
        try {
            inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("redis.properties");
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static String getProperty(String key, String defaultValue) {
        if (Objects.isNull(properties)) {
            loadProp();
        }
        String value = (String) properties.get(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return value;
    }
}
